import 'package:flutter/material.dart';
import 'package:shopping_group/entity/task_entity.dart';
import 'package:shopping_group/modle/person/bloc/my_money_bloc.dart';
import 'package:shopping_group/modle/play/entity/user_account_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/widget_utils.dart';

class ChangeViewIntegralPage extends StatefulWidget {
  @override
  _ChangeViewIntegralPageState createState() => _ChangeViewIntegralPageState();
}

class _ChangeViewIntegralPageState extends State<ChangeViewIntegralPage> {
  List<TaskEntity> _listEntity = [];
  UserAccountEntity _entity = UserAccountEntity();
  MyMoneyBloc _myMoneyBloc;

  @override
  void initState() {
    super.initState();
    _myMoneyBloc = MyMoneyBloc();
    _myMoneyBloc.initDate.add(null);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '购物积分',
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colours.blackground_609,
                      ),
                      child: Stack(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.centerLeft,
                            padding: EdgeInsets.symmetric(
                                horizontal: 16, vertical: 8),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  height: 16,
                                ),
                                WidgetUtils.getTextColorLineView('剩余积分',
                                    Colours.title_white, Dimens.FONT_SIZE_16),
                                _jfView(),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )),
                SizedBox(
                  height: 8,
                ),
                Expanded(
                  child: bodyView(),
                  flex: 3,
                ),
                SizedBox(
                  height: 8,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _jfView() {
    return StreamBuilder(
      stream: _myMoneyBloc.sendAllData,
      initialData: _entity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is UserAccountEntity) {
          _entity = object;
        }
        return Expanded(
            child: Container(
          alignment: Alignment.center,
          child: WidgetUtils.getTextColorView(
            _entity.integralAccount,
            Colours.title_white,
            Dimens.FONT_SIZE_30,
          ),
        ));
      },
    );
  }

  Widget bodyView() {
    return Container(
        color: Colours.transparent,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            Expanded(
              child: _showlistView(),
            ),
          ],
        ));
  }

  Widget _showlistView() {
    return Container(
      decoration: new BoxDecoration(
        color: Colors.white,
      ),
      child: ListView.builder(
        shrinkWrap: true,
        padding: const EdgeInsets.only(top: 4),
        itemCount: _listEntity.length,
        itemBuilder: buildItem,
      ),
    );
  }

  //ListView的Item
  Widget buildItem(BuildContext context, int index) {
    return Container(
      // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: _xunchaItem(_listEntity[index]),
    );
  }

  Widget _xunchaItem(TaskEntity entity) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.transparent,
      ),
      alignment: FractionalOffset.centerLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 4,
          ),
          WidgetUtils.itmeStyleView('到账时间', entity.time,
              contentAlignment: Alignment.centerRight,
              textColor: Colours.gray_91,
              contentColor: Colours.gray_91),
          SizedBox(
            height: 8,
          ),
          WidgetUtils.itmeStyleView('多人团奖励发放', '+ ¥${entity.price}',
              contentAlignment: Alignment.centerRight,
              contentColor: Colours.blackground_609,
              textColor: Colours.gray_91,
              isExpanded: false,
              isContentExpanded: true),
          SizedBox(
            height: 4,
          ),
          WidgetUtils.drawCodeDivider(height: 1),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
